package com.rd.student.wulian.wanggaobo.day0804;
public class CrossHole {
    public static void main(String[] args) {
        Mountain mo = new Mountain();
        for (int i = 1; i <= 10; i++) {
            Thread thread = new Thread(new Person("原始人" + i, mo));
            thread.start();
        }
    }
}

class Mountain {
    Object lock = new Object();
    int index = 1;

    public void enterHole(String name) {
        synchronized (lock) {
            int currentIndex = index++;
            System.out.println(name + "起洞，第" + currentIndex+"起洞");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "退出山洞。");
    }
}

class Person implements Runnable {
    String name;
    Mountain mountain;

    public Person(String name, Mountain mountain) {
        this.name = name;
        this.mountain = mountain;
    }

    @Override
    public void run() {
        mountain.enterHole(name);
    }
}